import requests
from bs4 import BeautifulSoup
class viewzyb():
    def __init__(self):
        self.pageIndex=1
        self.news=[]
        self.enable=False
    def get_news(self,page):
        page_news_list = []
        start_url = 'http://l.zhangyoubao.com/news/page-' + str(page) + '/'
        headers = {
            'User-Agent': "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"}
        start_html = requests.get(start_url, headers=headers)
        start_html.encoding = 'utf-8'
        news_list = BeautifulSoup(start_html.text, 'html5lib').find('ul', class_='article-list').find_all('h2')
        for list in news_list:
            news = list.find('a')
            href = news['href']
            title = news.get_text().strip()
            page_news_list.append(title + ':' + href)
        return page_news_list
    def loadnews(self):
        if self.enable==True:
            if (len(self.news)<2):
                page_news_list=self.get_news(self.pageIndex)
                if page_news_list:
                    self.news.append(page_news_list)
                    self.pageIndex +=1
    def getonenews(self,page_news_list):
        for news in page_news_list:
            input1 = input('请按回车查看下一条新闻：')
            self.loadnews()
            if input1 == 'q':
                self.enable = False
            print(news)
    def start(self):
        print('正在获取掌游宝新闻，按回车查看下一条新闻，按q退出')
        self.enable = True
        self.loadnews()
        nowpage = 0
        while self.enable:
            if len(self.news) > 0:
                page_news = self.news[0]
                del self.news[0]
                self.getonenews(page_news)
ZYB=viewzyb()
ZYB.start()
